﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace TeamBuy.AccessService
{
    public class TeamBuyOrderService : Common.ITeamBuyOrder
    {
        public System.Data.DataTable GetOrderInfoByID(int orderID)
        {
            return TeamBuy.AccessService.AccessHelper.GetDataSet("select * from vw_OrderInfo where OrderID=" + orderID);
        }

        public int CreateOrder(int userID, int dealID, int quantity, double totalPrice, bool isHadCard, string cardNumber, string mobileNumber)
        {
            int createOrder;
            createOrder = TeamBuy.AccessService.AccessHelper.ExecuteCommand("insert into C_Order(UserID, DealID, Quantity, TotalPrice, IsHadCard, CardNumber, MobileNumber) values(" + userID + "," + dealID + "," + quantity + "," + totalPrice + "," + isHadCard + ",'" + cardNumber + "','" + mobileNumber + "')");
            if (createOrder == 1)
            {
                return Convert.ToInt32(AccessHelper.GetScalar("select Top 1 OrderID from C_Order order by OrderID DESC"));
            }
            else
            {
                return 0;
            }
        }

        public int UpdateOrder(int orderID, int quantity, double totalPrice, bool isHadCard, string cardNumber, string mobileNumber)
        {
            int updateOrder;
            updateOrder = TeamBuy.AccessService.AccessHelper.ExecuteCommand("update C_Order set Quantity=" + quantity + ", TotalPrice=" + totalPrice + ", IsHadCard=" + isHadCard + ", CardNumber='" + cardNumber + "', MobileNumber='" + mobileNumber + "' where OrderID=" + orderID);
            if (updateOrder == 1)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }


        public int SetUserID(int orderID, int userID)
        {
            int getUserID = Convert.ToInt32(AccessHelper.GetScalar("select UserID from C_Order where OrderID=" + orderID));
            if (getUserID == 0)
            {
                int updateOrder;
                updateOrder = TeamBuy.AccessService.AccessHelper.ExecuteCommand("update C_Order set UserID=" + userID + " where OrderID=" + orderID);
                if (updateOrder == 1)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return -1;
            }
        }


        public System.Data.DataTable GetOrderInfoByIDAndUser(int orderID, int userID)
        {
            DataTable dt = TeamBuy.AccessService.AccessHelper.GetDataSet("select * from vw_OrderInfo where OrderID=" + orderID + " AND UserID=" + userID);
            if (dt == null)
            {
                return null;
            }
            else
            {
                return dt;
            }
        }


        public int SetOrderCurrentStatus(int orderID, int oldStatus, int currentStatus)
        {
            if (oldStatus == currentStatus)
            {
                return oldStatus;
            }
            else
            {
                int updateStatus;
                updateStatus = TeamBuy.AccessService.AccessHelper.ExecuteCommand("update C_Order set OrderStatus=" + currentStatus + " where OrderID=" + orderID);
                if (updateStatus == 1)
                {
                    return currentStatus;
                }
                else
                {
                    return -1;
                }
            }
        }
    }
}
